//
//  Game4TableViewCell.m
//  GOGOTravel
//
//  Created by dllo on 16/2/2.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "Game4TableViewCell.h"

@interface Game4TableViewCell ()

@property (nonatomic, assign) CGFloat realHeight;

@end

@implementation Game4TableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
       [self createCell];
        
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"yeJianMoShi"] isEqualToString:@"isDay"]) {
            self.backgroundColor = UIColorFromRGB(0xF0F0F0);
        }else{
            self.backgroundColor = [UIColor lightGrayColor];
        }

        
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"yeJianMoShi" object:nil];
    return self;
}

- (void)changeColor:(NSNotification *)notification{
    
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"yeJianMoShi"]isEqualToString:@"isNight"]) {
        self.backgroundColor = [UIColor lightGrayColor];
        _view3.backgroundColor = [UIColor lightGrayColor];
    }else{
        _view3.backgroundColor = [UIColor whiteColor];
        self.backgroundColor = [UIColor whiteColor];
    }
    
    
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeColor" object:nil];
    [_imageView1 release];
    [_imageView2 release];
    [_imageView3 release];
    [_imageView4 release];
    [_imageView5 release];
    [_imageView6 release];
    [_label1 release];
    [_label2 release];
    [_label4 release];
    [_lavel3 release];
    [_view1 release];
    [_view2 release];
    [_view3 release];
    [super dealloc];
}

- (void)createCell{
    
    self.imageView1 = [[UIImageView alloc] init];
    [self.contentView addSubview: _imageView1];
    [_imageView1 release];
    
    self.label1 = [[UILabel alloc] init];
    [self.contentView addSubview:_label1];
    [_label1 release];
    
    self.imageView2 = [[UIImageView alloc] init];
    [self.contentView addSubview:_imageView2];
    [_imageView2 release];
    
   
    self.imageView3 = [[UIImageView alloc] init];
    [self.contentView addSubview:_imageView3];
    [_imageView3 release];
    
    self.imageView4 = [[UIImageView alloc] init];
    [self.contentView addSubview:_imageView4];
    [_imageView4 release];
    //底层白色view
    self.view3 = [[UIView alloc] init];
    [self.contentView addSubview:_view3];
    [_view3 release];
    
    self.label2 = [[UILabel alloc] init];
    [self.contentView addSubview:_label2];
    [_label2 release];
    self.imageView5 = [[UIImageView alloc] init];
    [self.contentView addSubview:_imageView5];
    [_imageView5 release];
    
    self.lavel3 = [[UILabel alloc] init];
    [self.contentView addSubview:_lavel3];
    [_lavel3 release];
    
    
    self.imageView6 = [[UIImageView alloc] init];
    [self.contentView addSubview:_imageView6];
    [_imageView6 release];
    
    self.label4 = [[UILabel alloc] init];
    [self.contentView addSubview:_label4];
    [_label4 release];
    
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_button];
   
    
    
    self.button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_button3];
    
}

- (void)setModel:(Days *)model{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
    
    [self layoutCell];
    
}

- (void)layoutCell{
    
    //获取当前的cell的frame
    CGRect frame = [self frame];
    
    _imageView1.frame = CGRectMake(30, 15, 20, 20);
    _imageView1.image = [UIImage imageNamed:@"iconfont-shijian.png"];
    
    //    日期赋值
    NSString *day = [_model.day stringValue];
    NSString *str = [NSString stringWithFormat:@"第 %@ 天", day];
    NSString *date = [_model.local_time substringToIndex:10];
    NSString *result = [NSString stringWithFormat:@"%@ %@", str , date];
    _label1.frame = CGRectMake(50, 10, 150, 30);
    _label1.font = [UIFont systemFontOfSize:12];
    _label1.text = result;
    
    
    
    
        //获取图片的真实高度
        NSDictionary *dInfo = _model.photo_info;
        NSNumber *num1 = dInfo[@"h"];
        NSNumber *num2 = dInfo[@"w"];
        CGFloat height = [num1 floatValue];
        CGFloat width =  [num2 floatValue];
    if ([num1 integerValue] > 0 && [num2 integerValue] > 0) {
        
        //缩放后宽度固定为335；
        CGFloat scale = width / ([UIScreen mainScreen].bounds.size.width - 20);
        _realHeight = height / scale;
        _imageView2.frame = CGRectMake(10, 50, [UIScreen mainScreen].bounds.size.width - 20, _realHeight);
        
        
        [_imageView2 sd_setImageWithURL:[NSURL URLWithString:_model.photo] placeholderImage:[UIImage imageNamed:PLACE_HLODER]];
        
    }
    
    //label的自适应高度
    _label2.text = _model.text;
    
    NSString *str4 = _model.text;
    _label2.numberOfLines = 100;
    //设置字体样式
    NSDictionary *att = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    
    CGRect rect = [str4 boundingRectWithSize:CGSizeMake(self.frame.size.width - 35, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:att context:nil];
    
    _label2.frame = CGRectMake(15, _realHeight + 50 + 5 , [UIScreen mainScreen].bounds.size.width - 35, rect.size.height);
    
    
    _imageView5.frame = CGRectMake(20, _realHeight + 50 + 5 + rect.size.height + 10 , 20, 20);
    _imageView5.image = [UIImage imageNamed:@"iconfont-shijian.png"];
    _lavel3.frame =  CGRectMake(_imageView5.frame.size.width + 20 + 4 , _realHeight + 50 + 5 + rect.size.height + 4 , 150, 30);
    _lavel3.font = [UIFont systemFontOfSize:12];
    _lavel3.text = _model.local_time;
    
    _imageView6.frame = CGRectMake(_imageView5.frame.size.width + 20 + 4 + 150 , _realHeight + 50 + 5 + rect.size.height + 10, 25, 25);
    _imageView6.image = [UIImage imageNamed:@"iconfont-huoche.png"];
    NSDictionary *dic = _model.poi;
    _label4.frame = CGRectMake(_imageView5.frame.size.width + 20 + 4 + 150 + 20 + 10, _realHeight + 50 + 5 + rect.size.height  , 100, 50);
    _label4.text = dic[@"name"];
    _label4.font = [UIFont systemFontOfSize:13];
    _label4.numberOfLines = 2;
    
    _view3.frame = CGRectMake(10, _realHeight + 50 , [UIScreen mainScreen].bounds.size.width - 20, rect.size.height + 60);
    _view3.backgroundColor = [UIColor whiteColor];
    
    _button3.frame = CGRectMake(10, _realHeight + 50, self.frame.size.width - 20, rect.size.height + 60);
   
    
    //_realHeight + 50 + 5 + rect.size.height+ 65
    
    frame.size.height = _realHeight + 50 + 5 + rect.size.height+ 90;
    self.frame = frame;
}

#pragma mark button点击方法



- (void)layoutSubviews{
    [super layoutSubviews];
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
